package com.summer.campussct.controller;

import com.summer.campussct.annotation.Permission;
import com.summer.campussct.pojo.Role;
import com.summer.campussct.pojo.dto.RestResult;
import com.summer.campussct.service.IRoleService;
import com.summer.campussct.utils.PermRangeConstant;
import com.summer.campussct.utils.PermissionConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "角色管理API")
@RestController
@RequestMapping("role")
public class RoleController {

    @Resource
    private IRoleService roleService;

    @ApiOperation("获取角色列表")
    @GetMapping("list")
    @Permission(value = PermissionConstant.ROLE, range = PermRangeConstant.READ)
    public RestResult getList(){
        return roleService.getList();
    }

    @ApiOperation("获取可用角色列表")
    @GetMapping("list/enabled")
    @Permission(value = PermissionConstant.ROLE, range = PermRangeConstant.READ)
    public RestResult getListEnabled(){
        return roleService.getListEnabled();
    }

    @ApiOperation("添加角色")
    @PostMapping("add")
    @Permission(value = PermissionConstant.ROLE, range = PermRangeConstant.CREATE)
    public RestResult addRole(@RequestBody Role role){
        return roleService.addRole(role);
    }

    @ApiOperation("修改角色")
    @PutMapping("modify/{id}")
    @Permission(value = PermissionConstant.ROLE, range = PermRangeConstant.UPDATE)
    public RestResult modifyRole(@RequestBody Role role,
                                 @PathVariable("id") long id){
        return roleService.modifyRole(role, id);
    }

    @ApiOperation("删除角色")
    @DeleteMapping("delete/{id}")
    @Permission(value = PermissionConstant.ROLE, range = PermRangeConstant.DELETE)
    public RestResult deleteRole(@PathVariable("id") long id){
        return roleService.deleteRole(id);
    }

    @ApiOperation("获取角色的权限列表")
    @GetMapping("rolePermList/{id}")
    @Permission(value = PermissionConstant.ROLE, range = PermRangeConstant.READ)
    public RestResult rolePermList(@PathVariable("id") long id){
        return roleService.rolePermList(id);
    }

    @ApiOperation("给角色分配权限")
    @PutMapping("assignPrem/{id}")
    @Permission(value = PermissionConstant.ROLE, range = PermRangeConstant.READ)
    public RestResult assignPrem(@PathVariable("id") long id,
                                 @RequestBody Map<String, List<Long>> permIdListMap){
        return roleService.assignPrem(id, permIdListMap);
    }
}
